from sqlalchemy import Column, String, Integer, ForeignKey, DateTime, Text, Boolean, JSON
from sqlalchemy.orm import relationship
from enum import Enum as PyEnum

from app.models.base import Base


class NotificationType(PyEnum):
    TASK_ASSIGNED = "task_assigned"  # 任务分配
    TASK_COMPLETED = "task_completed"  # 任务完成
    TASK_OVERDUE = "task_overdue"  # 任务逾期
    RISK_IDENTIFIED = "risk_identified"  # 风险识别
    ISSUE_REPORTED = "issue_reported"  # 问题报告
    MILESTONE_REACHED = "milestone_reached"  # 里程碑达成
    PROJECT_STATUS_CHANGE = "project_status_change"  # 项目状态变更
    DOCUMENT_UPLOADED = "document_uploaded"  # 文档上传
    COMMENT_ADDED = "comment_added"  # 评论添加
    SYSTEM = "system"  # 系统通知


class NotificationPriority(PyEnum):
    LOW = "low"  # 低
    NORMAL = "normal"  # 正常
    HIGH = "high"  # 高
    URGENT = "urgent"  # 紧急


class Notification(Base):
    """通知模型"""
    title = Column(String(100), nullable=False)
    content = Column(Text, nullable=False)
    notification_type = Column(String(30), nullable=False)
    priority = Column(String(10), nullable=False, default=NotificationPriority.NORMAL.value)
    is_read = Column(Boolean, default=False)  # 是否已读
    read_at = Column(DateTime, nullable=True)  # 阅读时间
    metadata = Column(JSON, nullable=True)  # 元数据（如相关对象ID等）
    
    # 外键
    recipient_id = Column(Integer, ForeignKey("user.id"), nullable=False)  # 接收者
    sender_id = Column(Integer, ForeignKey("user.id"), nullable=True)  # 发送者（可能是系统）
    project_id = Column(Integer, ForeignKey("project.id"), nullable=True)  # 相关项目
    
    # 关系
    recipient = relationship("User", foreign_keys=[recipient_id], back_populates="notifications")
    sender = relationship("User", foreign_keys=[sender_id])
    project = relationship("Project")